function exp_COPAC()
%     h=figure(1);
%     exp_numcores_COPAC();
%     saveas(h,'figure_exp_numcores_COPAC.fig');
%     h=figure(2);
%     exp_numclusters_COPAC();
%     saveas(h,'figure_exp_numclustes_COPAC.fig');
    
    exp_test_COPAC();
    
end

function exp_test_COPAC()    
    data=D300F3();
    h=figure(3);
    drawData(data);
    [corrplanes,sups,clusters,numcores]=COPAC(data,80,6,2,0.1,0.2);
    for i=1:length(clusters)
        points=clusters{i};
        plot3(points(:,1),points(:,2),points(:,3),'black+');
        hold on;
    end
    saveas(h,'figure_exp_test_COPAC_80.fig');
    h=figure(4);
    drawData(data);
    [corrplanes,sups,clusters,numcores]=COPAC(data,90,6,2,0.1,0.2);
    for i=1:length(clusters)
        points=clusters{i};
        plot3(points(:,1),points(:,2),points(:,3),'black+');
        hold on;
    end
    saveas(h,'figure_exp_test_COPAC_90.fig');
    %filtercorrelations(corrplanes,originalplanes);
    corratts={};
    for i=1:length(corrplanes)
        corratts{i}=[1,2,3];
    end
    displaycorrelations(corrplanes,sups,corratts,0.01, 'test_COPAC.txt');
end

function exp_numcores_COPAC()
    data=D300F3();
    numcores_6 = test_numcores_COPAC(data, linspace(10,100,10), linspace(6,6,10));
    numcores_8 = test_numcores_COPAC(data, linspace(10,100,10), linspace(8,8,10));
    numcores_12 = test_numcores_COPAC(data, linspace(10,100,10), linspace(12,12,10));
    numcores=[numcores_6,numcores_8,numcores_12];
    save('exp_COPAC_numcores','numcores');
    x=linspace(10,100,10);
    plot(x,numcores_6,'b-+');
    hold on;
    plot(x,numcores_8,'r-*');
    hold on;
    plot(x,numcores_12,'g-o');
end

function exp_numclusters_COPAC()
    data=D300F3();
    numclusters_6 = test_numclusters_COPAC(data, linspace(10,100,10), linspace(6,6,10));
    numclusters_8 = test_numclusters_COPAC(data, linspace(10,100,10), linspace(8,8,10));
    numclusters_12 = test_numclusters_COPAC(data, linspace(10,100,10), linspace(12,12,10));
    numclusters=[numclusters_6,numclusters_8,numclusters_12];
    save('exp_COPAC_numclusters','numclusters');
    x=linspace(10,100,10);
    plot(x,numclusters_6,'b-+');
    hold on;
    plot(x,numclusters_8,'r-*');
    hold on;
    plot(x,numclusters_12,'g-o');
end

function [numcores]=test_numcores_COPAC(data, es, us)
    numcores=zeros(length(es),1);
    for i=1:length(es)
        [corrplanes,sups,clusters,core]=COPAC(data,es(i),us(i),2, 0.1,0.2);
        numcores(i)=core;
    end
end

function [numclusters]=test_numclusters_COPAC(data, es, us)
    numclusters=zeros(length(es),1);
    for i=1:length(es)
        [corrplanes,sups,clusters,core]=COPAC(data,es(i),us(i),2, 0.1,0.2);
        numclusters(i)=length(sups);
    end
end


function drawData(data)
    plot3(data(:,1),data(:,2),data(:,3),'r.');
    hold on;
    r=200;
    [x,y] = meshgrid(-r:2*r:r);
    z=3*x-5*y;
    surf(x,y,z,'FaceColor','cyan','FaceAlpha',0.2);
    hold on;
    z=2*x+y;
    surf(x,y,z,'FaceColor','yellow','FaceAlpha',0.2);
    hold on;
    z=-4*x-2*y+100;
    surf(x,y,z,'FaceColor','green','FaceAlpha',0.2);
    hold on;
    grid on;
end

function data=D300F3()
    numpoints = 100;
    r=200;
    x1=randint(1,numpoints,[-r,r]);
    y1=randint(1,numpoints,[-r,r]);
    z1=2*x1+y1+randn(1,numpoints); % 2x1+x2-x3=0
    x2=randint(1,numpoints,[-r,r]);
    y2=randint(1,numpoints,[-r,r]);
    z2=x2*3-y2*5+randn(1,numpoints); % 3x1-5x2-x3=0
    x3=randint(1,numpoints,[-r,r]);
    y3=randint(1,numpoints,[-r,r]);
    z3=-4*x3-2*y3+100+randn(1,numpoints); % 4x1+2x2+x3=100
    x=[x1,x2,x3];
    y=[y1,y2,y3];
    z=[z1,z2,z3];
    data=[ x',y',z'];
end
